import { defineStore } from 'pinia';
import axios from '@/plugins/axios';
import { ElMessage } from 'element-plus';

// 定义课程接口
export interface Course {
  id: number;
  title: string;
  description: string;
  teacherId: number;
  status: string;
  category: string;
}

// 定义创建课程请求接口
export interface CourseCreateRequest {
  title: string;
  description: string;
  teacherId: number;
  category: string;
}

// 定义更新课程请求接口
export interface CourseUpdateRequest {
  id: number;
  title: string;
  description: string;
  teacherId: number;
  category: string;
}

export const useCourseStore = defineStore('course', {
  state: () => ({
    courses: [] as Course[],
  }),

  actions: {
    // 获取所有课程
    async fetchCourses() {
      try {
        const response = await axios.get('/api/course');
        this.courses = response.data.data;
        return response.data.data;
      } catch (error) {
        ElMessage.error('获取课程列表失败');
        throw error;
      }
    },

    // 根据教师ID获取课程
    async fetchCoursesByTeacher(teacherId: number) {
      try {
        const response = await axios.get(`/api/course/teacher/${teacherId}`);
        return response.data.data;
      } catch (error) {
        ElMessage.error('获取教师课程失败');
        throw error;
      }
    },

    // 根据状态获取课程
    async fetchCoursesByStatus(status: string) {
      try {
        const response = await axios.get(`/api/course/status/${status}`);
        return response.data.data;
      } catch (error) {
        ElMessage.error('获取课程状态列表失败');
        throw error;
      }
    },

    // 获取单个课程详情
    async fetchCourseById(id: number) {
      try {
        const response = await axios.get(`/api/course/${id}`);
        return response.data.data;
      } catch (error) {
        ElMessage.error('获取课程详情失败');
        throw error;
      }
    },

    // 创建课程
    async createCourse(data: CourseCreateRequest) {
      try {
        const response = await axios.post('/api/course', data);
        this.courses.push(response.data.data);
        ElMessage.success('创建课程成功');
        return response.data.data;
      } catch (error) {
        ElMessage.error(error.response?.data?.message || '创建课程失败');
        throw error;
      }
    },

    // 更新课程
    async updateCourse(data: CourseUpdateRequest) {
      try {
        const response = await axios.put('/api/course', data);
        const index = this.courses.findIndex(course => course.id === data.id);
        if (index !== -1) {
          this.courses[index] = response.data.data;
        }
        ElMessage.success('更新课程成功');
        return response.data.data;
      } catch (error) {
        ElMessage.error(error.response?.data?.message || '更新课程失败');
        throw error;
      }
    },

    // 删除课程
    async deleteCourse(id: number) {
      try {
        await axios.delete(`/api/course/${id}`);
        this.courses = this.courses.filter(course => course.id !== id);
        ElMessage.success('删除课程成功');
      } catch (error) {
        ElMessage.error(error.response?.data?.message || '删除课程失败');
        throw error;
      }
    },
  },
});